package com.xiaoyushu.xiaoyushu.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.xiaoyushu.framework.biz.operationlog.aspect.ApiOperationLog;
import com.xiaoyushu.framework.common.response.Response;
import com.xiaoyushu.xiaoyushu.auth.alarm.AlarmInterface;
import com.xiaoyushu.xiaoyushu.auth.domain.dataobject.User;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestController {

    @Resource
    private AlarmInterface alarm;


    @GetMapping("/alarm")
    public String sendAlarm() {
        alarm.send("系统出错啦，犬小哈这个月绩效没了，速度上线解决问题！");
        return "alarm success";
    }
    //@NacosValue(value = "${rate-limit.api.limit}", autoRefreshed = true)
   // @NacosValue
    @NacosValue(value = "${rate-limit.api.limit}", autoRefreshed = true)
    private Integer limit;

    @GetMapping("/testLimit")
    public String testLimit() {
        return "当前限流阈值为: "
                + limit;
    }


    @GetMapping("/test")
    public Response<String> test() {
        return Response.success("hello world 小御书专栏");
    }

    @GetMapping("/test11")
    @ApiOperationLog(description = "测试接口")
    public Response<String> test1() {
        return Response.success("hello world 小御书专栏");
    }


/*    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口2")
    public Response<User> test2(@RequestBody @Validated User user) {

        return Response.success(user.builder()
                .nickName("犬小哈")
                .createTime(LocalDateTime.now())
                .build());
    }
    @PostMapping("/test3")
    @ApiOperationLog(description = "测试接口3")
    public Response<User> test3(@RequestBody @Validated User user) {

        return Response.success(user);
    }*/

    // 测试登录，浏览器访问： http://localhost:8080/user/doLogin?username=zhang&password=123456
    @RequestMapping("/user/doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8080/user/isLogin
    @RequestMapping("/user/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口2")
    public Response<User> test2(@RequestBody User user) {
        return Response.success(user);
    }
}
